<?php


namespace app\admin\controller\auth;


use app\admin\controller\Base;
use app\common\libray\Tree;
use think\facade\View;

class Rule extends Base
{
    protected $searchFields = 'id';
    protected $ruleList = [];


    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\common\model\AuthRule();
        $ruleList = $this->model->order('id asc')->select();
        Tree::instance()->init($ruleList);
        $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
        $ruledata = [0 => '顶级'];
        foreach ($this->rulelist as $k => &$v) {
            if (!$v['is_menu']) {
                continue;
            }
            $ruledata[$v['id']] = $v['title'];
        }
        unset($v);
        View::assign('rule_data', $ruledata);
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            $list = $this->rulelist;
            $total = count($this->rulelist);
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return View::fetch();
    }


}